package com.yihuu.repository;

import com.yihuu.entity.User;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.Date;

/**
 * Created by yuhui on 2017/8/15.
 */
public interface UserRepository extends CrudRepository<User,Integer> {

    User findByUnameAndUpassword(String uname,String upassword);

    @Query("select count(1) from User u where u.uphone = :phone")
    Long findByPhone(@Param("phone")String phone);

    @Query("select count(1) from User u where u.uname = :name")
    Long findByUname(@Param("name")String name);

    @Query("select u.question from User u where u.uname =:uname")
    String getQuestion(@Param("uname")String uname);

    User findByUnameAndQuestionAndAnswer(String uname,String question,String answer);

    @Modifying
    @Query("update User u set u.upassword=:upassword,u.updateTime=:updateTime where u.uname=:uname and u.answer=:answer and u.question =:question")
    Integer updateByUnameAndQuestionAndAndAnswer(@Param("uname") String uname, @Param("question") String question, @Param("answer") String answer,@Param("updateTime") Date updateTime,@Param("upassword") String upassword);
}
